package Gui.Dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import Gui.GameFrame;
import Gui.GamePanel;
import Main.Defines;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ViewLoansDialog extends JDialog {
	private JPanel jp;
	// private JLabel currentLoanLabel;
	// private JLabel currentCashLabel;
	private JLabel moneyInfo;
	private int originalCash;
	private int originalLoan;
	private JPanel labels;
	private JPanel moneyModifiersPanel;
	private JPanel confirmButtonsPanel;
	private final static int LOAN_AMOUNT = 1000;

	private void buildUIContents() {
		originalCash = Defines.CP().getCash();
		originalLoan = Defines.CP().getLoan();

		labels = new JPanel(new FlowLayout());
		moneyModifiersPanel = new JPanel();
		confirmButtonsPanel = new JPanel(new FlowLayout());

		// currentLoanLabel = new JLabel("Current Loan Amount: $"
		// + Defines.CP().getLoan() + "    ");
		// // TODO fix bug when loan hits 10k
		//		
		// currentCashLabel = new JLabel(String.format("Player %d Cash: %d",
		// Defines.CP().getIDPrintable(),
		// Defines.CP().getCash()));
		JLabel moneyInfo = new JLabel(String.format(
				"<html>Current Loan Amount: $%d<br>Player %d Cash: $%d",
				Defines.CP().getLoan(), Defines.CP().getIDPrintable(), Defines
						.CP().getCash()));
		JButton loan = new JButton(String.format(
				"<html>Loan $%d<br> from Bank", LOAN_AMOUNT));
		JButton payback = new JButton(String.format(
				"<html>Payback $%d<br> to Bank", LOAN_AMOUNT));
		JButton ok = new JButton("OK");
		JButton cancel = new JButton("Cancel");
		// add each component to its designated panel
		labels.add(moneyInfo);
		moneyModifiersPanel.add(loan);
		moneyModifiersPanel.add(payback);
		confirmButtonsPanel.add(ok);
		confirmButtonsPanel.add(cancel);

		// set action listeners
		loan.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Defines.CP().AddLoan(LOAN_AMOUNT);
				Defines.CP().AddCash(LOAN_AMOUNT);
				update();
			}
		});
		payback.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO check if player can afford payback
				// also check if player owes at least loan amount

				if (Defines.CP().getCash() >= LOAN_AMOUNT
						&& Defines.CP().getLoan() >= LOAN_AMOUNT) {
					Defines.CP().AddLoan(-LOAN_AMOUNT);
					Defines.CP().AddCash(-LOAN_AMOUNT);
				}
				update();

			}
		});
		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				// TODO setChangeInCash for loan amount
				dispose();
				GameFrame.getInstance().update();
			}
		});

		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Defines.CP().setCash(originalCash);
				Defines.CP().setLoan(originalLoan);
				GamePanel.getInstance().update();
				dispose();
				GameFrame.getInstance().update();
			}
		});

		// add panels to jp
		jp.add(labels);
		jp.add(moneyModifiersPanel);
		jp.add(confirmButtonsPanel);
	}

	public ViewLoansDialog(JFrame frame) {

		super(frame, "View Loans", true);
		jp = new JPanel();
		jp.setLayout(new GridLayout(0, 1, 5, 5)); // MAGIC NUMBER
		setLocationRelativeTo(frame);

		buildUIContents();
		add(jp);
		pack();
		// REFACTOR THIS! MAGIC NUMBER
		if (getTitle() != null) {
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + 75; // MAGIC NUMBER, 75 is
															// size of a
															// "typical" window
															// icon
			width = (int) Math.max(width * 1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
		}

		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	private void update() {
		GameFrame.getInstance().update();
		moneyInfo.setText(String.format(
				"<html>Current Loan Amount: $%d<br>Player %d Cash: $%d",
				Defines.CP().getLoan(), Defines.CP().getIDPrintable(), Defines
						.CP().getCash()));

	}
}
